{% extends "admin.html" %}

{% block js_includes %}		
<script src="/static/tiny_mce/tiny_mce_src.js" type="text/javascript"></script> 

<script type="text/javascript" charset="utf-8">
tinyMCE.init({
	mode : "textareas",
	theme : "advanced",
	plugins : "fullscreen, preview",
	theme_advanced_buttons1 : "bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,|,fontselect,fontsizeselect,|,undo,redo,|,link,image,forecolor,|,charmap,fullscreen,preview",
	theme_advanced_buttons2 : "",
	theme_advanced_buttons3 : "", 
	theme_advanced_toolbar_location : "top",
	theme_advanced_toolbar_align : "left",
	theme_advanced_statusbar_location : "bottom"
	
});	
</script>


{% endblock %}


{% block jsfuncs %}
	$('ed_toolbar_text').set('opacity',0);
	
	$('toggle').addEvent('click', function(e) {
		if (!tinyMCE.getInstanceById('text')) {
			tinyMCE.execCommand('mceAddControl', false, 'text');
			$('ed_toolbar_text').set('opacity',0);
		}
		else {
			tinyMCE.execCommand('mceRemoveControl', false, 'text');
			$('ed_toolbar_text').set('opacity',1);
		}
		return false;
	});
	
	
	var cats = $('categories').get('value');;
	$('cat_select').getElements('input').each(function(el){
	  if(cats.contains(el.value)){
			el.checked = true;
	  }
	});
	
	$('post_form_save').addEvent('click', function(e) {		
		new Event(e).stop();
		var log = $('load_img').setStyles({'display':'inline'});
		var data = $('post_form').toQueryString() + '&action=Save';
		var tiny = tinyMCE.getInstanceById('text');
		if (tiny && tiny.getContent()) {
			tiny.setProgressState(1); // Show progress
			if (!data.match(/text=.*?&/m)){
			data=data+"&"+"text=" + encodeURIComponent(tiny.getContent());
			}else{
			data = data.replace(/text=.*?&/m,"text=" + encodeURIComponent(tiny.getContent())+"&");
			}
		}
		var action = $('post_form').getProperty('action');
		var req = new Request.HTML({ url: action,
		 									  method: 'post',
											  evalScripts : true,
											  data: data,
												onComplete: function() {
													log.setStyles({'display':'none'});
													if (tiny) tiny.setProgressState(0);
												}
											}).send();
		return false;
	});

{% if post.draft or not post %}
	$('post_form_publish').addEvent('click', function(e) {
		new Event(e).stop();
		var log = $('load_img').setStyles({'display':'inline'});
		var data = $('post_form').toQueryString() + '&action=Publish';
		var tiny = tinyMCE.getInstanceById('text');
		if (tiny && tiny.getContent()) {
			tiny.setProgressState(1); // Show progress
			if (!data.match(/text=.*?&/m)){
			data=data+"&"+"text=" + encodeURIComponent(tiny.getContent());
			}else{
			data = data.replace(/text=.*?&/m,"text=" + encodeURIComponent(tiny.getContent())+"&");
			}	
		}
		var action = $('post_form').getProperty('action');
		var req = new Request.HTML({ url: action,
		 									  method: 'post',
											  evalScripts : true,
											  data: data,
												onComplete: function() {
													log.setStyles({'display':'none'});
													if (tiny) tiny.setProgressState(0);
												}
											}).send();
		return false;
	});
	
{% if errordesc %}
	(function(){Growl.Error({title:'{{ errordesc }}', text: ''}); }).delay(500);
{% endif %}	
	
{% endif %}
	
{% endblock %}

{% block content %}
	<h3>{{ title }}</h3>
	<div id="new_post">
	<form action="{{ action }}" method="post" id="post_form" accept-charset="utf-8">
		<h4>标题（*）:</h4>
		<input type="text" name="title" value="{{ post.title }}" id="title" class="npi" /><br />
		<h4>标签（以","来分隔）:</h4>
		<input type="text" name="tags" value="{{ post.tag_string }}" id="tags" class="npi80" />
		Tags append:<input type="checkbox" name="tagsapp" checked="yes" id="tagsapp" /><br />
		<h4>友好的链接:</h4>
		<input type="text" name="permalink" value="{{ post.permalink }}" id="permalink" class="npi"
		{% if permalinknotedit %}disabled{% endif %}><br />
		<h4>摘要:</h4>
		<input type="text" name="excerpt" value="{{ post.excerpt }}" id="excerpt" class="npi"><br />
		<h4>正文（*）:</h4>
		<input type="hidden" name="categories" value="{{ p_categories }}" id="categories" />
		<script type="text/javascript">edToolbar('text');</script>
		<textarea name="text" id="text" rows="20" cols="40" id="text" class="npi">{{ post.content }}</textarea>
		<a href="#" id="toggle">切换编辑器</a>
		<input type="checkbox" name="comments_enabled" value="yes" id="comments_enabled" {% if post.comments_enabled or not post %} checked="checked"		
		{% endif %} /> 允许评论
		<input type="hidden" name="key" value="{{ key }}" id="post_key">
		<p>{% if post.draft or not post %}<input type="submit" id="post_form_publish" value="发布" name="actiona">{% endif %}
			<input type="submit" id="post_form_save" value="随手保存" name="actiona">
			<img src="/static/images/loading.gif" style="display:none;" id="load_img" /></p>
	</form>
	</div>
{% endblock %}
	
{% block sidebar %}
	<div id="sidebar">
		<h4>选择文章分类</h4>
			<ul id="cat_select">
	{% for category in post_categories %}
				<li><input type="checkbox" name="some_name" value="{{ category.key }}" onchange="javascript:cat_change();"/>{{ category.category_name }}</li>	
	{% endfor %}
		</ul>	
	</div>
	<script type="text/javascript" charset="utf-8">
		var cat_change = function() {
			value='';
			$('cat_select').getElements('input').each(function(el){
			  if(el.checked){
					value = value + el.get('value') + '+';
			  }
			});
			$('categories').value=value;
		};
	</script>
{% endblock %}